package com.example.demo.kafka;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.Date;
@Component
@Slf4j
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    //发送消息方法
    public void send() {


        for (int i = 0; i<100;i++) {

            Message message = new Message();
            message.setId(System.currentTimeMillis());
            message.setMsg(UUID.randomUUID().toString()+"--"+i+"");
            message.setSendTime(new Date());
            System.out.println("+++++++++++++++++++++  message = "+gson.toJson(message));
            kafkaTemplate.send("zhisheng", gson.toJson(message));
        }
    }
}